/**
 * this is what find the members by a keyword that the user input.
 */

function searchMenu(address){
	var search;
	$('.find input[type="text"]').focus(function(){
		$('.find input[type="text"]').keydown(function(e){
			if($('.find input[type="text"]').val() != 0 || e.keyCode == 13){
				clearTimeout(search);
				search = setTimeout(function(){
					$('.find .searchResult .searchLoading span').css('display', 'none');
					$('.find .searchResult .searchLoading img').css('display', 'block');
					$('.find .searchResult .searchLoading').css('display', 'block');
					$('.find .searchResult').css('display', 'block');

					$.ajax({
						url: address+"searchMember.action"
							, type: "POST"
								, dataType: "json"
									, data: { keyword: $('.find input[type="text"]').val() }
					, success: function(json){
						var searchSize = json.searchList.length;
						if(searchSize == 0){
							$('.find .searchResult .searchLoading img').css('display', 'none');
							$('.find .searchResult .searchLoading span').css('display', 'block');
						}
						else{
							$('.find .searchResult .searchLoading').css('display', 'none');
							$('.find .searchResult').css('height', (searchSize*70)+'px');

							var html = "";
							for(var i = 0; i < searchSize; i++){
								html += "<a href='../member/goHome.action?email="+json.searchList[i].email+"'><div class='result'>";
								html += "<div class='profileResult'>";
								if(json.searchList[i].savedFile != null){
									html += "<img src='../photoData/"+json.searchList[i].savedFile+"'>";
								}
								else {
									html += "<img src='../image/profile.png'>";
								}
								html += "</div>";
								html += "<div class='infoResult'>";
								html += "<span class='resultName'>"+json.searchList[i].name+"</span>";
								html += "<span class='resultNickname'>("+json.searchList[i].nickname+")</span><br>";
								html += "<span class='resultEmail'>"+json.searchList[i].email+"</span>";
								html += "</div></div></a>";
							}
							$('.find .searchResult .showResult').html(html);
							$('div.profileResult').imgLiquid();

							for(var i = 0; i < searchSize; i++){
								$("div.result[data-num='"+i+"']").css('margin-top', (70*i)+'px');
							}
						}
					}
					});

				}, 1000);
			}
		});
	});
	$('.find input[type="text"]').blur(function(){
		var leave = setInterval(function(){
			if($('div.infoResult').is('hover') == false){
				clearInterval(leave);
				clearTimeout(search);
				$('.find .searchResult .showResult').html('');
				$('.find input[type="text"]').val('');
				$('.find .searchResult').css('display', 'none');
			}
		}, 1000);
	});
}
